package cn.tedu.management.community.service.impl;



import cn.tedu.management.community.dao.repository.CommunityRepository;
import cn.tedu.management.community.pojo.dto.CommunityParam;
import cn.tedu.management.community.pojo.dto.CommunityQuery;
import cn.tedu.management.community.pojo.entity.Community;
import cn.tedu.management.community.pojo.vo.CommunityVO;
import cn.tedu.management.community.service.CommunityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityRepository communityRepository;

    @Override
    public List<CommunityVO> select(CommunityQuery communityQuery) {
        QueryWrapper<Community> queryWrapper=getCommunityQueryWrapper(communityQuery);
        List<Community> lists = communityRepository.list(queryWrapper);
        return lists.stream().map(list -> {
            CommunityVO communityVO = new CommunityVO();
            BeanUtils.copyProperties(list, communityVO);
            return communityVO;
        }).collect(Collectors.toList());
    }

    private QueryWrapper<Community> getCommunityQueryWrapper(CommunityQuery communityQuery) {
        QueryWrapper<Community> queryWrapper=new QueryWrapper<>();
        if(communityQuery.getId()!=null){
            queryWrapper.eq("id", communityQuery.getId());
        }
        if(communityQuery.getName()!=null&&!"".equals(communityQuery.getName())){
            queryWrapper.like("name", communityQuery.getName());
        }
        if(communityQuery.getNickname()!=null&&!"".equals(communityQuery.getNickname())){
            queryWrapper.like("nickname", communityQuery.getNickname());
        }
        if(communityQuery.getCode()!=null&&!"".equals(communityQuery.getCode())){
            queryWrapper.like("code", communityQuery.getCode());
        }
        if(communityQuery.getType()!=null&&!"".equals(communityQuery.getType())){
            queryWrapper.like("type", communityQuery.getType());
        }
        if(communityQuery.getScale()!=null){
            queryWrapper.eq("scale", communityQuery.getScale());
        }
        if(communityQuery.getArea()!=null){
            queryWrapper.eq("area", communityQuery.getArea());
        }
        if(communityQuery.getAddress()!=null&&!"".equals(communityQuery.getAddress())){
            queryWrapper.like("address", communityQuery.getAddress());
        }
        if(communityQuery.getUpdateTime()!=null){
            queryWrapper.eq("update_time", communityQuery.getUpdateTime());
        }
        if(communityQuery.getFireUserId()!=null){
            queryWrapper.eq("fire_user_id", communityQuery.getFireUserId());
        }
        if(communityQuery.getSafeUserId()!=null){
            queryWrapper.like("safe_user_id", communityQuery.getSafeUserId());
        }
        if(communityQuery.getPropertyUserId()!=null){
            queryWrapper.like("property_user_id", communityQuery.getPropertyUserId());
        }
        return queryWrapper;
    }

    @Override
    public CommunityVO selectById(Long id) {
        Community community = communityRepository.getById(id);
        CommunityVO communityVO = new CommunityVO();
        BeanUtils.copyProperties(community, communityVO);
        return communityVO;
    }

    @Override
    public void addCommunity(CommunityParam communityParam) {
        Community community= param2Entity(communityParam);
        community.setCreateTime(new Date());
        communityRepository.save(community);
    }

    @Override
    public void delete(Long id) {
        communityRepository.removeById(id);
    }

    @Override
    public void update(CommunityParam communityParam) {
        UpdateWrapper<Community> updateWrapper = getCommunityUpdateWrapper(communityParam);
        updateWrapper.set("update_time", new Date());
        communityRepository.update(null,updateWrapper);
    }

    private UpdateWrapper<Community> getCommunityUpdateWrapper(CommunityParam communityParam) {
        UpdateWrapper<Community> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id", communityParam.getId());
        if(communityParam.getName()!=null&&!"".equals(communityParam.getName())){
            updateWrapper.set("name", communityParam.getName());
        }
        if(communityParam.getNickname()!=null&&!"".equals(communityParam.getNickname())){
            updateWrapper.set("nickname", communityParam.getNickname());
        }
        if(communityParam.getCode()!=null&&!"".equals(communityParam.getCode())){
            updateWrapper.set("code", communityParam.getCode());
        }
        if(communityParam.getRemark()!=null&&!"".equals(communityParam.getRemark())){
            updateWrapper.set("remark", communityParam.getRemark());
        }
        if(communityParam.getType()!=null&&!"".equals(communityParam.getType())){
            updateWrapper.set("type", communityParam.getType());
        }
        if(communityParam.getScale()!=null){
            updateWrapper.set("scale", communityParam.getScale());
        }
        if(communityParam.getArea()!=null){
            updateWrapper.set("area", communityParam.getArea());
        }
        if(communityParam.getAddress()!=null&&!"".equals(communityParam.getAddress())){
            updateWrapper.set("address", communityParam.getAddress());
        }
        if(communityParam.getFireUserId()!=null){
            updateWrapper.set("fire_user_id", communityParam.getFireUserId());
        }
        if(communityParam.getSafeUserId()!=null){
            updateWrapper.set("safe_user_id", communityParam.getSafeUserId());
        }
        if(communityParam.getPropertyUserId()!=null){
            updateWrapper.set("property_user_id", communityParam.getPropertyUserId());
        }
        return updateWrapper;
    }

    private Community param2Entity(CommunityParam communityParam) {
        Community community=new Community();
        BeanUtils.copyProperties(communityParam,community);
        return community;
    }
}
